//
//  LGPersonalInputView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/14.
//  编辑个人简介、擅长领域

#import "LGPersonalInputView.h"

@interface LGPersonalInputView()<UITextViewDelegate>

/**   */
@property (nonatomic , strong)UILabel *numberLabel;


@end

@implementation LGPersonalInputView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.placeHolder = @"请输入...";
        [self addSubview:self.contentTF];
        [self addSubview:self.numberLabel];
        [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(self).offset(viewPix(12));
            make.right.equalTo(self).offset(-viewPix(12));
            make.bottom.equalTo(self).offset(-viewPix(42));
            make.height.equalTo(@(viewPix(50)));
        }];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-viewPix(12));
            make.bottom.equalTo(self);
            make.height.equalTo(@(viewPix(36)));
        }];
    }
    return self;
}

-(void)setContent:(NSString *)content{
    self.contentTF.text = content;
    if(content.length == 0 || [content isEqualToString:self.placeHolder]){
        self.contentTF.text = self.placeHolder;
        self.contentTF.textColor = [UIColor colorWithString:@"#9595A0"];
    }else{
        self.contentTF.textColor = [UIColor colorWithString:@"#2B2A37"];
    }
    if(self.content.length == 0){
        [self textViewDidChange:self.contentTF];
    }
    _content = content;
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.contentTF.textColor = [UIColor colorWithString:@"#2B2A37"];
    if([textView.text isEqualToString:self.placeHolder]){
        self.contentTF.text = @"";
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    self.content = textView.text;
//    NSString *content = textView.text;
//    if(content.length == 0||[textView.text isEqualToString:self.placeHolder]){
//        self.contentTF.text = self.placeHolder;
//        self.contentTF.textColor = [UIColor colorWithString:@"#9595A0"];
//    }else{
//        self.contentTF.textColor = [UIColor colorWithString:@"#2B2A37"];
//    }
//    if(self.delegate && [self.delegate respondsToSelector:@selector(inputContentChanged:content:)]){
//        [self.delegate inputContentChanged:self.questionId content:content];
//    }
}

- (void)textViewDidChange:(UITextView *)textView{
    NSString *lang = textView.textInputMode.primaryLanguage; // 键盘输入模式
    NSString *content = textView.text;
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) { // 没有高亮
            if (textView.text.length>500) { // 输入的文字大于设定的长度  这里设置的是200个字符
                textView.text = [textView.text substringToIndex:500];
            }else{
                textView.text = textView.text;
            }
            self.numberLabel.text = [NSString stringWithFormat:@"%ld / 500",textView.text.length];
            if(textView.text.length>0){
                textView.attributedText = [textView.text attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#2B2A37"]} lineSpacing:5];
            }
            CGSize maxSize = CGSizeMake(self.contentTF.size.width, MAXFLOAT);
            CGSize fitSize = [textView sizeThatFits:maxSize];
            CGFloat viewHeight = self.contentTF.bounds.size.height;
            CGFloat newHeight = fitSize.height;
            NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
            if(newHeight > viewPix(50) && newHeight != viewHeight){
        //        NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
                [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.equalTo(@(newHeight));
                }];
                if(self.delegate && [self.delegate respondsToSelector:@selector(inputContentChanged:viewTag:)]){
                    [self.delegate inputContentChanged:newHeight viewTag:self.tag];
                }
            }
        }
    }else{
        if (textView.text.length>500) {
            textView.text = [textView.text substringToIndex:500];
        }else{
            textView.text = textView.text;
        }
        self.numberLabel.text = [NSString stringWithFormat:@"%ld / 500",textView.text.length];
        if(textView.text.length>0){
            textView.attributedText = [textView.text attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#2B2A37"]} lineSpacing:5];
        }
        CGSize maxSize = CGSizeMake(Screen_W-viewPix(56), MAXFLOAT);
        CGSize fitSize = [textView sizeThatFits:maxSize];
        CGFloat viewHeight = self.contentTF.bounds.size.height;
        CGFloat newHeight = fitSize.height;
        NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
        if(newHeight > viewPix(50) && newHeight != viewHeight){
    //        NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
            [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(newHeight));
            }];
            if(self.delegate && [self.delegate respondsToSelector:@selector(inputContentChanged:viewTag:)]){
                [self.delegate inputContentChanged:newHeight viewTag:self.tag];
            }
        }
    }
}

-(UITextView *)contentTF{
    if(!_contentTF){
        _contentTF = [[UITextView alloc]init];
        _contentTF.tintColor = [UIColor colorWithString:@"#666666"];
        _contentTF.textColor = [UIColor colorWithString:@"#666666"];
        _contentTF.textAlignment = NSTextAlignmentLeft;
        _contentTF.font = LGFont(14);
        _contentTF.delegate = self;
        _contentTF.scrollEnabled = NO;
    }
    return _contentTF;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"0 / 500" colorString:@"#BFBFBF" textFont:LGFont(12) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _numberLabel;
}




@end
